﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class UserPassword : Form
    {
        MainForm form;
        public UserPassword(MainForm  mm)
        {
            InitializeComponent();
            form=mm;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
        }


      
        private void UserPassword_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            txtUserName.Text = MainForm.Operator;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtnewpasswold.Text == "" || txtnewpassword1.Text == "")
            {              
                MessageBox.Show("密码不能为空!"); return;
            }
            string pwd = txtoldpassword.Text;
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bt = Encoding.Default.GetBytes(pwd);
            bt = md5.ComputeHash(bt);
            pwd = BitConverter.ToString(bt).Replace("-", "");

            operatorInfo info = new operatorInfo();
            Model.operatorInfo infoModel = new Model.operatorInfo();
            infoModel = info.GetModelList("username='" + txtUserName.Text + "'")[0];
            if (pwd != infoModel.password)
            {
                MessageBox.Show("旧密码输入错误!"); return;
            }
            if (txtnewpasswold.Text == txtnewpassword1.Text)
            {
                string pwd1 = txtnewpassword1.Text;
                MD5 md51 = new MD5CryptoServiceProvider();
                byte[] bt1 = Encoding.Default.GetBytes(pwd1);
                bt1 = md5.ComputeHash(bt1);
                pwd1 = BitConverter.ToString(bt1).Replace("-", "");
                infoModel.password = pwd1;
                if (info.Update(infoModel))
                {
                    MessageBox.Show("密码修改成功!");
                }
            }
            else
            {
                MessageBox.Show("两次密码输入不一致!");
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }

        }
        int flag = 0;
        private void UserPassword_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }
    }
}